<template>

    <el-container class="home-container">
        <!-- 头部区域-->
        <el-header>
            <div>
                <img src="../assets/img/log.png" alt="" class="home-log">
                <span>卡姿兰大眼睛</span>
            </div>
            <el-button type="info" @click="logout">退出登录</el-button>
        </el-header>

        <el-container>
            <!-- 左侧菜单栏区域-->
            <el-aside :width="isFold ? '64px' : '200px' ">
                <div class="toggle-button" @click="fold">|||</div>
                <!-- 菜单内容-->
                <el-menu class="el-menu-vertical-demo"
                         background-color="#333744"
                         text-color="#fff"
                         active-text-color="#409EFF"
                         unique-opened
                         :collapse="isFold"
                         :collapse-transition="false" router
                         :default-active="this.$route.path">
                    <!-- 一级菜单-->
                    <el-submenu :index="item.id + ''" v-for="(item) in menuList" :key="item.id">
                        <template slot="title">
                            <!-- 菜单图标-->
                            <i class="el-icon-location"></i>
                            <!-- 菜单文字-->
                            <span v-text="item.authName">导航一</span>
                        </template>
                        <!-- 二级菜单-->
                        <el-menu-item :index="'/'+sumItem.path+''" v-for="(sumItem) in item.children" :key="sumItem.id"
                                      >
                            <!-- 菜单图标-->
                            <i class="el-icon-menu"></i>
                            <!-- 菜单文字-->
                            <span slot="title" v-text="sumItem.authName">选项一</span>
                        </el-menu-item>

                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 主体区域-->
            <el-main>
               <router-view></router-view>
            </el-main>
        </el-container>

    </el-container>
</template>

<script>
    import Welcome from "./welcome";
    export default {
        name: "home",
        components: {Welcome},
        data() {
            return {
                menuList: [],
                isFold: false,
            }
        },
        methods: {
            // 用户点击退出登录,清空token,返回登录页面
            logout() {
                window.sessionStorage.clear();
                this.$router.replace('/login');
            },
            // 初始化左侧菜单栏
            initLeftMenu() {
                this.$http.get('/menus').then(res => {
                    res = res.data;
                    if (res.meta.status != 200) return this.$message.error(res.meta.msg);
                    this.menuList = res.data

                }).catch(err => {
                    this.$message.error(err);
                })
            },
            // 开启菜单栏折叠
            fold(){
                this.isFold = !this.isFold;
            }
        },
        // 生命周期函数 data和methods加载完成的时候
        // 初始化左侧菜单栏
        created() {
            this.initLeftMenu();
        }
    }
</script>

<style lang="less" scoped>
    .home-container {
        height: 100%;
    }

    .el-header {
        background-color: #373d41;
        display: flex;
        /*左右贴边对其*/
        justify-content: space-between;
        /*左边贴边*/
        padding-left: 5px;
        align-items: center;
        /*文本颜色, 和大小*/
        color: #fff;
        font-size: 20px;
        > div {
            display: flex;
            align-items: center;
            span {
                margin-left: 15px;
            }
        }
    }

    .el-aside {
        background-color: #333744;
        .el-menu {
            border-right: none;
        }
    }

    .el-main {
        background-color: #eaedf1;
    }

    .iconfont {
        /*向右偏移*/
        margin-right: 10px;
    }

    .toggle-button {
        background-color: #4a5064;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }

    .home-log {
        width: 120px;
        padding: 2px;
    }

</style>